package atessitura;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import custom.KevinSlider;
import custom.KevinSliderUser;
import net.miginfocom.swing.MigLayout;


public class ControlPanel extends JPanel implements KevinSliderUser{
	private TFController control;
	private float defaultPercent;
	
	//private JSlider percent;
	private KevinSlider percent;
	
	private KevinSlider time;
	private JLabel leftTimeLabel;
	private JLabel rightTimeLabel;
	
	private JComboBox difficulty;
	private double[] difficultyLevels={0.25,0.5,1,1.5,2};
	
	private JComboBox trackOptions;
	
	public ControlPanel(TFController controller)
	{
		control=controller;
		defaultPercent=(float)0.24;
		
		String[] tracks=getTrackOptions(control.getNumTracks());

		//Create the combo box, select item at index 4.
		//Indices start at 0, so 4 specifies the pig.
		trackOptions = new JComboBox(tracks);
		trackOptions.setFocusable(false);
		trackOptions.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				control.setCurrentGuitarTrack(trackOptions.getSelectedIndex());
			}
			
		});
		add(trackOptions, "cell 0 0");
		
		/*percent=new JSlider();
		percent.setValue((int)(defaultPercent*100));
		control.setPercentage(defaultPercent);
		percent.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				control.setPercentage((float)(percent.getValue())/100);
				control.repaintGPanel();
			}
			
		});
		*/
		percent=new KevinSlider(this, (int)(defaultPercent*100));
		control.setPercentage(defaultPercent);
		
		
		setLayout(new MigLayout("alignx center"));
		add(percent, "cell 1 0");//,alignx center,aligny top");
		/*time=new JSlider();
		time.setValue(0);
		time.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				System.out.println("time to be set"+time.getValue());
				control.setTime(time.getValue());
			}
			
		});*/
		time=new KevinSlider(this, 0);
		leftTimeLabel=new JLabel(control.getStringTime());
		leftTimeLabel.setForeground(Color.WHITE);
		rightTimeLabel=new JLabel(control.getStringTimeLength());
		rightTimeLabel.setForeground(Color.WHITE);
		add(leftTimeLabel);
		add(time);
		add(rightTimeLabel);
		
		String[] difficultyStrings = { "Beginner ("+difficultyLevels[0]+"x)", "Easy ("+difficultyLevels[1]+"x)", "Normal Speed ("+difficultyLevels[2]+"x)", "Hard ("+difficultyLevels[3]+"x)", "Expert ("+difficultyLevels[4]+"x)"};

		difficulty = new JComboBox(difficultyStrings);
		difficulty.setSelectedIndex(2);
		difficulty.setFocusable(false);
		difficulty.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				control.setTempoFactor(difficultyLevels[difficulty.getSelectedIndex()]);
			}
			
		});
		add(difficulty);
		
		JToggleButton looper=new JToggleButton("Loop", false);
		looper.setFocusable(false);
		looper.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				JToggleButton loop=(JToggleButton)e.getSource();
				control.setLooping(loop.isSelected());
				
			}
			
		});
		add(looper);
		
		
		JButton octaveUp=new JButton("+8");
		octaveUp.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				octaveUp();
				
			}
			
		});
		JButton octaveDown=new JButton("-8");
		octaveDown.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				octaveUp();
			}
			
		});
		//add(octaveUp);
		//add(octaveDown);
	}
	
	public void updateTime(double percentPlayed)
	{
		//System.out.println("why you no update "+ percentPlayed);
		time.setValue((int)(percentPlayed*100));
		time.repaint();
		leftTimeLabel.setText(control.getStringTime());
		leftTimeLabel.repaint();
	}
	
	//Called when new song is chosen
	public void setTimes() {
		leftTimeLabel.setText(control.getStringTime());
		rightTimeLabel.setText(control.getStringTimeLength());
		leftTimeLabel.repaint();
		rightTimeLabel.repaint();
		time.setValue(0);
		time.repaint();
	}
	
	public void octaveUp()
	{
		control.octaveUp();
	}
	
	public void octaveDown()
	{
		control.octaveDown();
	}
	
	public String[] getTrackOptions(int numT)
	{
		String[] tracks=new String[numT];
		for (int i=0;i<numT;i++)
		{
			tracks[i]="Track "+(i+1);
		}
		return tracks;
	}
	
	public void setUpTracks()
	{
		String[] tracks=getTrackOptions(control.getNumTracks());

		remove(trackOptions);
		trackOptions = new JComboBox(tracks);
		trackOptions.setFocusable(false);
		trackOptions.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				control.setCurrentGuitarTrack(trackOptions.getSelectedIndex());
			}
			
		});
		add(trackOptions, "cell 0 0");
		repaint();
		revalidate();
	}

	@Override
	public void kevinSliderActivated(KevinSlider changed) {
		if (changed.equals(percent))
		{
			control.setPercentage((float)(percent.getValue())/100);
			control.repaintGPanel();
		}
		else if (changed.equals(time))
		{
			//double newTime=time.getValue()
			leftTimeLabel.setText(control.getStringTime());
			leftTimeLabel.repaint();
			control.setTime(time.getValue());
			control.repaintGPanel();
		}
	}

	public void resetDifficulty()
	{
		difficulty.setSelectedIndex(2);
		confirmDifficulty();
	}
	public void confirmDifficulty()
	{
		control.setTempoFactor(difficultyLevels[difficulty.getSelectedIndex()]);
	}
	

}
